/*
 * bsp_init.c
 *
 *  Created on: 2023年6月29日
 *      Author: mcealt_DS
 */

#include "bsp_init.h"
#include "bsp_sysclk.h"
//#define CONFIG_KIT_SILABAS_EFR32XG23
#if defined (CONFIG_KIT_SILABAS_EFR32XG23)
#include "mx25flash_spi.h"
#endif
#include "sl_device_init_dcdc.h"
#include "sl_device_init_lfxo.h"
#include "sl_device_init_hfxo.h"
#include "sl_device_init_dpll.h"
#include "em_chip.h"
#include "em_gpio.h"
#include "em_emu.h"
#include "bsp_rtc.h"
#include "bsp_rf.h"
#include "net_protocol.h"
#include "net_rolling_code.h"
#include "app_key_value.h"
#include "device.h"
#include "device_type.h"

void bsp_em_init(void)
{
  // Enable voltage downscaling in EM2/3 (VSCALE0)
  EMU_EM23Init_TypeDef em23Init = EMU_EM23INIT_DEFAULT;
  em23Init.vScaleEM23Voltage = emuVScaleEM23_LowPower;

  // Initialize EM23 energy modes
  EMU_EM23Init(&em23Init);
}


void bsp_device_init_clocks(void)
{
#ifdef CONFIG_DPLL
  CMU_CLOCK_SELECT_SET(SYSCLK, HFRCODPLL);
#else
  CMU_CLOCK_SELECT_SET(SYSCLK, HFXO);
#endif
#if defined(_CMU_EM01GRPACLKCTRL_MASK)
#ifdef CONFIG_DPLL
  CMU_CLOCK_SELECT_SET(EM01GRPACLK, HFRCODPLL);
#else
  CMU_CLOCK_SELECT_SET(EM01GRPACLK, HFXO);
#endif
#endif
#if defined(_CMU_EM01GRPBCLKCTRL_MASK) //TODO:保留该选项,防止未来做芯片替换时遗漏该选项
#ifdef CONFIG_DPLL
  CMU_CLOCK_SELECT_SET(EM01GRPBCLK, HFRCODPLL);
#else
  CMU_CLOCK_SELECT_SET(EM01GRPBCLK, HFXO);
#endif
#endif
#if defined(_CMU_EM01GRPCCLKCTRL_MASK)
#ifdef CONFIG_DPLL
  CMU_CLOCK_SELECT_SET(EM01GRPCCLK, HFRCODPLL);
#else
  CMU_CLOCK_SELECT_SET(EM01GRPCCLK, HFXO);
#endif
#endif
//TODO:其余的CLK在各自的模块有做init

}

void bsp_init(void)
{
    CHIP_Init();                       /* chip             */
    sl_device_init_dcdc();
    DS_sysclkInit();                   /* clock init             */
    CMU_ClockEnable(cmuClock_GPIO, true);
#if defined (CONFIG_KIT_SILABAS_EFR32XG23)
    MX25_sleep();
#endif

    sl_device_init_lfxo();
    sl_device_init_hfxo();

#ifdef CONFIG_DPLL
    sl_device_init_dpll();
    bsp_device_init_clocks();
#endif
    DS_debugInit();                    /* bsp debug usart init     */
#ifdef BSP_ENABLE_COM
    DS_comInit();
#endif

    bsp_em_init();
    initBURTC();
    initSYSRTC();	
    nvm_device_init();
    rf_init();
    app_key_value_init();
}
